package com.zret.oasys.operation.emp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zret.oasys.dbtools.PageData;
import com.zret.oasys.entity.Dept;
import com.zret.oasys.entity.Emp;
import com.zret.oasys.entity.EmpDeptPosition;
import com.zret.oasys.entity.Position;
import com.zret.oasys.json.entity.GetTableDataJson;

/**
 * 遍历所有员工的方法
 * 
 * @author L.lawliet
 */
public class GetEmps implements EmpServletOperation {
	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置json编码
		response.setContentType("application/json;charset=utf-8");
		String key = "";
		if (request.getParameter("key") != null) {
			key = request.getParameter("key");
		}
		System.out.println(key);
		// 获取page参数
		int page = Integer.valueOf(request.getParameter("page"));
		// 获取pagesize参数
		int pageSize = Integer.valueOf(request.getParameter("limit"));
		// 2.获取查询到PageData转换为list
		PageData<EmpDeptPosition> pd = empService.getEmpDeptPositionByPage(page, pageSize,key);
		// 3.创建输出流
		PrintWriter out = response.getWriter();
		// 4.创建Gson对象并且关联deptid添加deptname属性至json
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		GetTableDataJson<EmpDeptPosition> getEmpsJson = new GetTableDataJson<EmpDeptPosition>(0, "", pd.getTotalCount(), pd.getData());
		// System.out.println(getEmpsJson);
		// 5.将查询到的员工list转为字符串
		String jsonString = gson.toJson(getEmpsJson);
		// 6.控制台测试打印
		System.out.println(jsonString);
		// 7.传输给jsp页面
		out.print(jsonString);
		// 8.关闭out
		out.close();
		
	}

}
